//
//  DYShopDecorateCell.m
//  kafuMerchant
//
//  Created by 帝云科技 on 2018/9/20.
//  Copyright © 2018年 帝云科技. All rights reserved.
//

#import "DYShopDecorateCell.h"
#import "DYShopDecorateCollectionCell.h"

@interface DYShopDecorateCell ()<UICollectionViewDelegate, UICollectionViewDataSource,TZImagePickerControllerDelegate>

@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)TZImagePickerController *imagePicker;


@end

@implementation DYShopDecorateCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
-(void)dy_initUI
{
    [super dy_initUI];
    
    self.sideMargin=BILIWIDTH(10);
    [self xhq_noneSelectionStyle];
    
    self.dataArray=[NSMutableArray array];
    
    self.titleLabel=({
        UILabel *label=[Utils labelWithTitleFontSize:12 textColor:[UIColor xhq_content] alignment:0];
        label;
    });
  
    _collectionView = ({
        UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake((kScreenWidth-BILIWIDTH(60))/5,(kScreenWidth-BILIWIDTH(60))/5);
        layout.minimumInteritemSpacing = BILIWIDTH(5);
        layout.minimumLineSpacing =BILIWIDTH(10);
        layout.headerReferenceSize = CGSizeMake(0, 0);
//        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        
        UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(BILIWIDTH(10), 0, kScreenWidth-BILIWIDTH(20), BILIHEIGHT(0))
                                                             collectionViewLayout:layout];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.pagingEnabled=YES;
        collectionView.showsHorizontalScrollIndicator=NO;
        [collectionView registerClass:[DYShopDecorateCollectionCell  class] forCellWithReuseIdentifier:@"DYShopDecorateCollectionCell"];
        
        if (@available(iOS 11.0, *)) {
            collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        collectionView.backgroundColor = [UIColor whiteColor];
        collectionView;
    });
    
    [self addSubview:self.collectionView];
    [self addSubview:self.titleLabel];
    
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(BILIWIDTH(10));
        make.top.equalTo(BILIHEIGHT(10));
    }];
    
    [self.collectionView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(BILIWIDTH(10));
        make.right.equalTo(BILIWIDTH(-10));
        make.bottom.equalTo(BILIWIDTH(-1));
        make.top.equalTo(self.titleLabel.bottom).offset(BILIHEIGHT(6));
    }];
  
}

#pragma mark - UICollectionViewDataSource methods
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.imageNumber==1) {
        return 1;
    }
    if (self.dataArray.count>0) {
        return self.dataArray.count+1;
    }
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DYShopDecorateCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DYShopDecorateCollectionCell" forIndexPath:indexPath];
    
   
    @weakify(self);
    cell.addBlock = ^{
        @strongify(self);
        self.imagePicker.maxImagesCount=self.imageNumber;
        [[DYAppCommon getCurrentVC] presentViewController:self.imagePicker animated:YES completion:nil];
        [self.imagePicker setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
            @strongify(self);
            
            [self.dataArray removeAllObjects];
            self.dataArray=[photos mutableCopy];
            !self.imageBlock?:self.imageBlock(self.dataArray);
        }];
    };
    
    cell.deleteBlock = ^{
        @strongify(self);
        [self.dataArray removeObjectAtIndex:indexPath.row];
        !self.imageBlock?:self.imageBlock(self.dataArray);
    };
    
    if (self.dataArray.count>0) {
        if (indexPath.row==self.dataArray.count) {
            cell.showImageView.image=IMG(@"img_upload");
            cell.deleteButton.hidden=YES;
        }else{
            cell.showImageView.image=self.dataArray[indexPath.row];
            cell.deleteButton.hidden=NO;
        }
    }else{
        cell.showImageView.image=IMG(@"img_upload");
        cell.deleteButton.hidden=YES;
    }
    
   
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

}

-(void)setDataArray:(NSMutableArray *)dataArray
{
    _dataArray=dataArray;

    [self.collectionView reloadData];
}
-(TZImagePickerController *)imagePicker
{
    if (!_imagePicker) {
        _imagePicker=[[TZImagePickerController alloc]initWithMaxImagesCount:1 delegate:self];
        _imagePicker.preferredLanguage=@"zh-Hans";
        _imagePicker.allowPickingVideo=NO;
        _imagePicker.naviBgColor=[UIColor whiteColor];
        _imagePicker.naviTitleColor=[UIColor xhq_aTitle];
        _imagePicker.barItemTextColor=[UIColor xhq_aTitle];
        _imagePicker.showSelectBtn=YES;
    }
    return _imagePicker;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end


@interface DYShopDecorateSeletedCell ()

@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UIButton *arrayButton1;
@property (nonatomic,strong)UIButton *arrayButton2;

@end

@implementation DYShopDecorateSeletedCell

-(void)dy_initUI
{
    [super dy_initUI];
    
    self.hideSeparatorLabel=YES;
    [self xhq_noneSelectionStyle];
    
    self.titleLabel=({
        UILabel *label=[Utils labelWithTitleFontSize:12 textColor:[UIColor xhq_content] alignment:0];
        label.text=@"排列方式";
        label;
    });
    
    self.arrayButton1=({
        UIButton *button=[Utils createBtnWithType:UIButtonTypeCustom backgroundColor:ClearColor action:@selector(buttonClick:) target:self title:@"  每排1张" image:@"icon_choose_n" font:14 textColor:[UIColor xhq_aTitle]];
        [button setImage:IMG(@"icon_choose_s") forState:UIControlStateSelected];
        button.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
        button;
    });
    
    self.arrayButton2=({
        UIButton *button=[Utils createBtnWithType:UIButtonTypeCustom backgroundColor:ClearColor action:@selector(buttonClick:) target:self title:@"  每排2张" image:@"icon_choose_n" font:14 textColor:[UIColor xhq_aTitle]];
        [button setImage:IMG(@"icon_choose_s") forState:UIControlStateSelected];
        button.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
        button;
    });
    
    [self addSubview:self.titleLabel];
    [self addSubview:self.arrayButton1];
    [self addSubview:self.arrayButton2];
    
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(BILIWIDTH(10));
        make.top.equalTo(BILIHEIGHT(10));
    }];
    
    [self.arrayButton1 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(BILIWIDTH(10));
        make.top.equalTo(self.titleLabel.bottom).offset(BILIHEIGHT(6));
        make.bottom.equalTo(BILIHEIGHT(-3));
    }];
    
    [self.arrayButton2 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.arrayButton1.right).offset(BILIWIDTH(15));
        make.centerY.equalTo(self.arrayButton1);
        make.height.equalTo(self.arrayButton1);
    }];
}
-(void)buttonClick:(UIButton *)button
{
    self.arrayButton1.selected=NO;
    self.arrayButton2.selected=NO;
    button.selected=!button.selected;
}

@end
